﻿var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
  .getService(Components.interfaces.mozIJSSubScriptLoader);
loader.loadSubScript("chrome://remember/content/common.js");

// Annotation and IO service
var annotationService = Components.classes["@mozilla.org/browser/annotation-service;1"]
  .getService(Components.interfaces.nsIAnnotationService);
var ioservice = Components.classes["@mozilla.org/network/io-service;1"]
  .getService(Components.interfaces.nsIIOService);

function contextPopupShowing() {
  gContextMenu.showItem("setRememberAnno", !currentlyRemembered());
  gContextMenu.showItem("removeRememberAnno", currentlyRemembered());
}

// This is the main thingie
var rememberExtension = {
  init: function() {
    var menu = document.getElementById("contentAreaContextMenu");
    menu.addEventListener("popupshowing", contextPopupShowing, false);

    var appcontent = document.getElementById("appcontent");   // browser
    if(appcontent)
      appcontent.addEventListener("load", this.onPageLoad, true);
    var messagepane = document.getElementById("messagepane"); // mail
    if(messagepane)
      messagepane.addEventListener("load", this.onPageLoad, true);
  },

  onPageLoad: function(aEvent) {
    var doc = aEvent.originalTarget;
    var current = doc.location.href;
    var justnow = Date.now();
 
    try {
      var uri = ioservice.newURI(current, null, null);
      var count = annotationService.getAnnotationInt64(uri, "remember_to_surf/count");
      count = count + 1;
      annotationService.setAnnotationInt64(uri, "remember_to_surf/count", count, 0, 0);
      annotationService.setAnnotationInt64(uri, "remember_to_surf/last", justnow, 0, 0);
    } catch(e) {
      // Do nooooothing!
    }
  }
}

// This is the function for creating a ranking list and displaying it
function doVooDoo() {
  var theseSites = new Array();
  var pages = annotationService.getPagesWithAnnotation("remember_to_surf/count", {});
  
  for (var i = 0; i < pages.length; i++) {
    var uri = pages[i];
    var first = annotationService.getAnnotationInt64(uri, "remember_to_surf/first");
    var last = annotationService.getAnnotationInt64(uri, "remember_to_surf/last");
    var count = annotationService.getAnnotationInt64(uri, "remember_to_surf/count");
    
    // Function resides in common.js
    makeTheseSites(uri.spec, first, last, count, theseSites);
  }
  // Function resides in common.js
  sortAndDisplay(theseSites);
}

// Set Annotation
function setRememberAnno() {
  var current = getBrowser().currentURI;
  var justnow = Date.now();
  annotationService.setAnnotationInt64(current, "remember_to_surf/first", justnow, 0, 0);
  annotationService.setAnnotationInt64(current, "remember_to_surf/last", justnow, 0, 0);
  annotationService.setAnnotationInt64(current, "remember_to_surf/count", 1, 0, 0);
  alert(content.document.title);
}

// Remove Annotation
function removeRememberAnno() {
  var current = getBrowser().currentURI;
  annotationService.removeAnnotation(current, "remember_to_surf/first");
  annotationService.removeAnnotation(current, "remember_to_surf/last");
  annotationService.removeAnnotation(current, "remember_to_surf/count");
}

// Returns true if it allready is marked by this extension
function currentlyRemembered() {
  var current = getBrowser().currentURI;
  if (annotationService.hasAnnotation(current, "remember_to_surf/count")) {
    return true;
  } else {
    return false;
  }
}